package com.ttbj.product.truck.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.enums.EnumUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ttbj.api.dto.Page;
import com.ttbj.api.dto.truck.TruckCustomDto;
import com.ttbj.api.dto.truck.TruckDto;
import com.ttbj.api.exception.BusinessException;
import com.ttbj.api.truck.TruckService;
import com.ttbj.constant.GenerateNumConstants;
import com.ttbj.constant.PlatFormErrorCode;
import com.ttbj.constant.annotation.SystemServiceLog;
import com.ttbj.constant.truck.TruckDeleteEnum;
import com.ttbj.core.util.RegexUtil;
import com.ttbj.core.util.TokenUtils;
import com.ttbj.truck.dao.TruckMapper;



@Service("truckService")
public class TruckServiceImpl implements TruckService{
	
	@Autowired
	private TruckMapper truckMapper;

	
	
	@Override
	@Cacheable(value="findInfoUsedOrder",key="#truckId")
	public TruckCustomDto findTruckInfoById(Long truckId) throws BusinessException {
		if (null == truckId) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		TruckCustomDto truckDto = this.truckMapper.queryTruckInfoById(truckId);
		return truckDto;
	}

	@Override
	public List<TruckCustomDto> findTruckInfoByPage(TruckCustomDto truckCustomDto, Page page) throws BusinessException {
		List<TruckCustomDto> truckCustomDtos = this.truckMapper.queryTruckInfoByPage(truckCustomDto, page);
		return truckCustomDtos;
	}

	@Override
	public Integer findTruckInfoByPageCount(TruckCustomDto truckCustomDto) throws BusinessException {
		Integer count = this.truckMapper.queryTruckInfoByPageCount(truckCustomDto);
		return count;
	}

	@Override
	@Transactional
	@SystemServiceLog(description="增加货车信息")
	public void addTruckInfo(TruckDto truckDto) throws BusinessException {
		if (null == truckDto) {
			throw new BusinessException("操作错误", PlatFormErrorCode.DATA_SECURITY.code());
		}
		//如果车牌号为空或者格式不正确
		if (StringUtils.isBlank(truckDto.getTruckCarnum()) || !RegexUtil.isCarnumberNO(truckDto.getTruckCarnum())) {
			throw new BusinessException("车牌号不正确！", PlatFormErrorCode.DATA_SECURITY.code());
		}
		//设置货车的编号
		truckDto.setTruckNum(TokenUtils.getNo(GenerateNumConstants.TRUCK));
		//设置插入时间
		truckDto.setTruckInsTime(new Date());
		//执行添加操作
		int result = this.truckMapper.insertTruckInfo(truckDto);
		if (result == 0) {
			throw new BusinessException("未知错误，添加货车信息失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}
	
	@Override
	@Cacheable(value="usedEdit", key="#truckId")
	public TruckDto findTruckInfoUsedEdit(Long truckId) throws BusinessException {
		TruckDto truckDto = this.truckMapper.queryTruckInfoUsedEdit(truckId);
		return truckDto;
	}
	
	@Override
	@Transactional
	@CacheEvict(value= {"findInfoUsedOrder","usedEdit"},key="#truckId")
	@SystemServiceLog(description="修改货车信息")
	public void editTruckInfo(TruckDto truckDto) throws BusinessException {
		if (null == truckDto) {
			throw new BusinessException("操作错误", PlatFormErrorCode.DATA_SECURITY.code());
		}
		//执行更新操作
		int result = this.truckMapper.updateTruckInfo(truckDto);
		if (result == 0) {
			throw new BusinessException("错误，更新货车信息失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}

	@Override
	@Transactional
	@CacheEvict(value= {"usedEdit"},key="#truckId")
	@SystemServiceLog(description="禁用或启用货车状态")
	public void editTruckDisable(Integer isDel, Long truckId) throws BusinessException {
		if (null != truckId && (TruckDeleteEnum.del.getCode() == isDel || TruckDeleteEnum.not_del.getCode() == isDel)) {
			//执行更改禁用状态操作
			int result = this.truckMapper.updateTruckIsDel(isDel, truckId);
			if (result == 0) {
				throw new BusinessException("错误，更新货车状态失败！", PlatFormErrorCode.DATA_SECURITY.code());
			}
		}else {
			throw new BusinessException("非法操作，更改失败！", PlatFormErrorCode.DATA_SECURITY.code());
		}
	}

	@Override
	public List<TruckCustomDto> findTruckCarnumAndEntity(Long[] truckIds) throws BusinessException {
		if (null == truckIds || truckIds.length == 0) {
			throw new BusinessException("参数有误", PlatFormErrorCode.PARAM_IS_NULL.code());
		}
		List<TruckCustomDto> truckCustomDtos = this.truckMapper.queryTruckCarnumAndEntity(truckIds);
		return truckCustomDtos;
	}
}
